public class Hero {
    private String heroName;
    private boolean bAlive = true;    //存活状态,创建英雄后默认是存活状态
    private int attackPower;  //攻击力
    private int attackRange;  //攻击范围
    private int defensePower; //防御力
    private int healthPoints; //生命值
    private int coordinate_x; //位置，x坐标
    private int coordinate_y; //位置，x坐标
    private int speed;        //移动速度

    public Hero(String heroName, int attackPower,
                int attackRange, int defensePower,
                int healthPoints, int coordinate_x,
                int coordinate_y, int speed) {
        this.heroName = heroName;
        this.attackPower = attackPower;
        this.attackRange = attackRange;
        this.defensePower = defensePower;
        this.healthPoints = healthPoints;
        this.coordinate_x = coordinate_x;
        this.coordinate_y = coordinate_y;
        this.speed = speed;
    }

    public int getCoordinate_x() {
        return this.coordinate_x;
    }

    public int getCoordinate_y() {
        return this.coordinate_y;
    }

    /**
     * 校验生命值是否大于0，并更新生存状态
     */
    private void updateAlive() {
        if (this.healthPoints > 0) return;

        healthPoints = 0;
        bAlive = false;
    }

    /**
     * 受到攻击会被调用该方法更新生命值
     * @param attackPower 敌方攻击力
     */
    private void attacked(int attackPower) {
        int actualAttack = attackPower - this.defensePower;
        if (actualAttack < 0) {
            System.out.println("攻击力太低了，根本不掉血！！");
            return;
        }
        this.healthPoints = this.healthPoints - actualAttack;
        updateAlive();
    }

    /**
     * 普通攻击方法
     * @param enemy 敌军
     */
    public void attack(Hero enemy) {
        int enemyCoordinateX = enemy.getCoordinate_x();
        int enemyCoordinateY = enemy.getCoordinate_y();
        int coordinateX = this.getCoordinate_x();
        int coordinateY = this.getCoordinate_y();

        int xDistance = enemyCoordinateX - coordinateX;
        int yDistance = enemyCoordinateY - coordinateY;

        double distance = Math.sqrt(xDistance*xDistance + yDistance*yDistance);

        // 距离不够，结束
        if (distance > attackRange) {
            return;
        }

        // 攻击对方,敌方受到攻击
        enemy.attacked(this.attackPower);
    }

    /**
     * 移动方法，参数为负数代表往坐标轴反方向移动
     * @param x x方向移动距离
     * @param y y方向移动距离
     */
    public void move(int x, int y) {
        this.coordinate_x = this.coordinate_x + x;
        this.coordinate_y = this.coordinate_y + y;

        // 位置不能大于100，也不能小于0，这个是地图边界
        if (this.coordinate_x > 100) this.coordinate_x = 100;
        if (this.coordinate_y > 100) this.coordinate_y = 100;
        if (this.coordinate_x < 0) this.coordinate_x = 0;
        if (this.coordinate_y < 0) this.coordinate_y = 0;
    }

    @Override
    public String toString() {
        return "Hero{" +
                "heroName='" + heroName + '\'' +
                ", bAlive=" + bAlive +
                ", attackPower=" + attackPower +
                ", attackRange=" + attackRange +
                ", defensePower=" + defensePower +
                ", healthPoints=" + healthPoints +
                ", coordinate_x=" + coordinate_x +
                ", coordinate_y=" + coordinate_y +
                ", speed=" + speed +
                '}';
    }
}